<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Passwordreset_model extends CI_Model {

	var $table_name = 'a_reset_password';
	var $expire_days = 10;

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * create_password_reset - inserts a new password reset record
	 */
	function create_password_reset($id, $key)
	{
		$data = array('user_id' => $id, 'key' => $key);
		if ($this->db->insert($this->table_name, $data)) {
			return $this->db->insert_id();
		}
		return NULL;
	}
	
	/**
	 * get_password_reset - will only return a user id if it exists AND it is within 10 days
	 */
	function get_password_reset($key)
	{
		$this->db->where('key', $key);
		$this->db->where('DATEDIFF(NOW(), date_requested) <', $this->expire_days);

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
			return NULL;
	}
	
	/**
	 * delete_password_reset - removes a key once used
	 */
	function delete_password_reset($key)
	{
		$this->db->delete($this->table_name, array('key' => $key));
	}
	
 }
 ?>